package com.wyh.controller.groupfour.admin;

import com.wyh.common.exception.ResourceNotFoundException;
import com.wyh.common.result.Result;
import com.wyh.domain.dto.groupfour.myproprietorDTO.PaymentRecordsDTO;
import com.wyh.domain.vo.gruopfour.myProprietorVO.*;

import com.wyh.domain.vo.gruopfour.threemeterVO.PageResult;
import com.wyh.service.appservice.ChargeService;
import com.wyh.service.appservice.IMyProprietorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.wyh.common.enums.MyProprietorEnum.LEASED_RESOURCE;
import static com.wyh.common.enums.ResultCodeEnum.RESOURCE_NOT_ERROR;

//我的业主
@RestController
@RequestMapping("admin/myProprietor")
@Api(tags = "我的业主")
@Slf4j
public class MyProprietorController {


    @Autowired
    private IMyProprietorService myProprietorService;

    @Autowired
    private ChargeService chargeService;

    @ApiOperation("客户基本信息")
    @GetMapping("/basicInformation")
    public Result<BasicInformation> getBasicInformation(@ApiParam("客户id") @RequestParam(value = "id") Integer id) {

        BasicInformation basicInformation = myProprietorService.getBasicInformation(id);
        return Result.success(basicInformation);
    }


    @ApiOperation("客户资源信息")
    @GetMapping("/resourceInformation")
    public Result<List<ResourceInformationVO>> getResourceInformation(@RequestParam(value = "id") @ApiParam("客户id") Integer id) {
        log.info("id:{}",id);
        return Result.success(myProprietorService.getResourceInformation(id));
    }

    @ApiOperation("我的业主")
    @GetMapping
    public Result<PageResult<MyProprietorVO>> getMyProprietor(@ApiParam("客户姓名") String customerName,
                                                              @ApiParam("客户电话") String telephone,
                                                              @RequestParam(defaultValue = "1") int pageNum,
                                                              @RequestParam(defaultValue = "10") int pageSize) {
        List<Integer> ids = chargeService.getAllResourceIds(LEASED_RESOURCE.getCode());
        if (ids == null || ids.isEmpty()) {
            throw new ResourceNotFoundException(RESOURCE_NOT_ERROR.getCode(),RESOURCE_NOT_ERROR.getMsg());
        }
        PageResult<MyProprietorVO> myProprietor = myProprietorService.getMyProprietor(customerName, telephone, ids, pageNum, pageSize);
        return Result.success(myProprietor);
    }


    @ApiOperation("客户缴费记录")
    @GetMapping("/paymentRecords")
    public Result<PageResult<PaymentRecordsVO>> getPaymentRecords(PaymentRecordsDTO paymentRecordsDTO) {
        //获取所有资源id
        List<Integer> ids = chargeService.getAllResourceIds(LEASED_RESOURCE.getCode());
        if (ids == null || ids.isEmpty()) {
            throw new ResourceNotFoundException(RESOURCE_NOT_ERROR.getCode(),RESOURCE_NOT_ERROR.getMsg());
        }
        log.info("资源ids:{}",ids);
        PageResult<PaymentRecordsVO> paymentRecords = myProprietorService.getPaymentRecords(paymentRecordsDTO, ids);
        return Result.success(paymentRecords);
    }

    @ApiOperation("账单详情")
    @GetMapping("/billDetails")
    public Result<BillDetailVO> getBillDetails(@RequestParam(value = "id") @ApiParam("账单id") Integer id) {
        return Result.success(myProprietorService.getBillDetails(id));
    }

    @ApiOperation("客户信息详情")
    @GetMapping("/customerDetail")
    public Result<CustomerInfoDetailVO> getChangeResource(@RequestParam(value = "id") @ApiParam("资源id") Integer id) {
        CustomerInfoDetailVO customerInfoDetailVO = new CustomerInfoDetailVO();
        customerInfoDetailVO.setResourceVO(myProprietorService.getChangeResource(id));
        customerInfoDetailVO.setDepositVOList(myProprietorService.getDeposit(id));
        customerInfoDetailVO.setSpecialBalanceVOList(myProprietorService.getSpecialBalance(id));
        customerInfoDetailVO.setBillListVOList(myProprietorService.getBillList(id));

        return Result.success(customerInfoDetailVO);
    }

}
